from flask_script import Manager, Server

from app import app
from app.database import Message
from app.database import db

manager = Manager(app)


@manager.command
def create_db():
    """create database used"""
    db.create_all()


@manager.command
def insert_db():
    m1 = Message("yh", "hello")
    message = m1
    print(message.nickname + " " + message.message + " " + str(message.submit_time))
    db.session.add(m1)
    db.session.commit()


@manager.command
def query_db():
    messages = Message.query.all()
    for message in messages:
        print(message.nickname + " " + message.message + " " + str(message.submit_time) + " " + str(message.uuid))

manager.add_command("runserver",
                    Server(host="127.0.0.1", port=5000, use_debugger=True))


if __name__ == "__main__":
    manager.run()
